package com.linln.modules.store.repository;

import java.util.List;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import com.linln.modules.store.domain.Storeinfo;
import com.linln.modules.system.repository.BaseRepository;

/**
 * @author hct
 * @date 2019/09/12
 */
public interface StoreinfoRepository extends BaseRepository<Storeinfo, Long> {
	
//根据经纬度查询数据并排序
String sql="SELECT*,ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN((#{lat} * PI() / 180 - latitude * PI() / 180) / 2),2) + COS(40.0497810000 * PI() / 180) * COS(latitude * PI() / 180) * POW(SIN(( #{lng} * PI() / 180 - longitude * PI() / 180  ) / 2),2))) * 1000  ) AS dis  FROM  baseinfo ORDER BY dis DESC";



@Modifying
@Transactional
@Query("update Storeinfo set storeStatus = ?1  where id = ?2 ")
public Integer updateStoreStatus(Integer storeStatus, Long id);


@Modifying
@Transactional
@Query("update Storeinfo set readnum = readnum+1  where id = ?1 ")
public Integer updateIsReadAddById(Long id);

@Modifying
@Transactional
@Query("update Storeinfo set sharenum = sharenum+1  where id = ?1 ")
public Integer updateIsShareAddById(Long id);


@Modifying
@Transactional
@Query("update Storeinfo set commentnum = commentnum+1  where id = ?1 ")
public Integer updateReplyAddById(Long id);

@Modifying
@Transactional
@Query("update Storeinfo set collectnum = collectnum+1  where id = ?1 ")
public Integer updateIsCollectAddById(Long id);

@Modifying
@Transactional
@Query("update Storeinfo set collectnum = collectnum-1  where id = ?1 ")
public Integer updateIsCollectSubById(Long id);

@Modifying
@Transactional
@Query("update Storeinfo set zannum = zannum+1  where id = ?1 ")
public Integer updateIsZanAddById(Long id);

@Modifying
@Transactional
@Query("update Storeinfo set zannum = zannum-1  where id = ?1 ")
public Integer updateIsZanSubById(Long id);

@Transactional
@Query(value="select count(*)  from Storeinfo where area = ?1 ")
public Integer getCountByArea(String area);


@Transactional
@Query(value="select count(*)  from Storeinfo where area = ?1 and to_days(create_date) = to_days(now()) ")
public Integer getCountByAreaAndToday(String area);



@Transactional
@Query(value="select * from  storeinfo where  area=?1 and istop=1 and store_status=1 and now()<end_time and now() between start_time and  date_add(start_time, interval day minute) ",nativeQuery=true)
public List<Storeinfo> queryListByArea(String area);

@Transactional
@Query(value="select * from  storeinfo where  member_id=?1  and pay_status=1 and store_status=1 ",nativeQuery=true)
public List<Storeinfo> queryListByMemberId(Long memberid);

public  Storeinfo findByOrderId(String orderId);


//查看代理商可以查看的信息列表
@Transactional
@Query(value="select * from  storeinfo where  area in ( ?1 ) and istop=1 and store_status=1 and now()<end_time ",nativeQuery=true)
public List<Storeinfo> queryListByAreaNew(String area);



/**
 * 确认已经支付，修改状态
 * @param orderId
 * @return
 */
@Modifying
@Transactional
@Query("update Storeinfo set storeStatus=1, payStatus =1  where orderId = ?1 and payStatus=0")
public Integer updatePayStatus( String orderId);

/**
 * 确认退款，修改状态
 * @param orderId
 * @return
 */
@Modifying
@Transactional
@Query("update Storeinfo set payStatus =2  where orderId = ?1 and payStatus=1")
public Integer updateRefundStatus( String orderId);


@Modifying
@Transactional
@Query("update Storeinfo set store_star =?1,readnum =?2  where id = ?3")
public Integer updateStoreStar(Integer storeStar,Integer readnum,Long id);


}